import 'package:client/model/chat_item_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:client/common/event.dart';
import 'package:client/common/fun.dart';
import 'package:client/model/chatroom_member_model.dart';
import 'package:client/provider/app_provider.dart';
import 'package:client/provider/chatroom_member_provider.dart';
import 'package:provider/provider.dart';

class ChatroomOnline extends StatefulWidget {
  ChatroomOnline({super.key});

  @override
  _ChatroomOnlineState createState() => _ChatroomOnlineState();
}

class _ChatroomOnlineState extends State<ChatroomOnline> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    var view = Consumer<ChatroomMemberProvider>(
      builder: (context, onlineProvider, child) {
        List<ChatroomMemberModel> onlineList = onlineProvider.getList();

        return ListView.builder(
            itemCount: onlineList.length,
            itemBuilder: (context, index) {
              var loginUser = AppProvider.getUser();
              var onlineUser = onlineList[index];
              Widget item;
              if (loginUser.uuid == onlineUser.uuid) {
                item = TextButton(
                  onPressed: null,
                  child: Text(onlineUser.name),
                );
              } else {
                item = TextButton(
                    onPressed: () {
                      showDialog<String>(
                        context: context,
                        builder: (BuildContext context) => AlertDialog(
                          // title: const Text('AlertDialog Title'),
                          content: const Text("操作"),
                          actions: <Widget>[
                            TextButton(
                              onPressed: () {
                                //关闭
                                Navigator.of(context).pop();

                                var uuidList = [
                                  loginUser.uuid,
                                  onlineUser.uuid
                                ];
                                var talkId = Fun.buildTalkId(uuidList);
                                //创建私聊
                                var model = ChatItemModel();
                                model.name = onlineUser.name;
                                model.uuid = onlineUser.uuid;
                                model.talkId = talkId;
                                model.online = 1;
                                model.infoTip = 0;
                                bus.emit(EventBusSet.chatAdd, model);
                                bus.emit(EventBusSet.chatChange, model);
                                //跳转私聊
                                AppProvider.appPage.jumpToPage(1);
                              },
                              child: const Text('聊天'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              child: const Text('取消'),
                            ),
                          ],
                        ),
                      );
                    },
                    child: Text(onlineUser.name));
              }
              var itemView = Container(
                padding: const EdgeInsets.only(top: 2, bottom: 2),
                // decoration: BoxDecoration(
                //     border: Border(
                //         bottom: BorderSide(color: Colors.lightBlue[300]!))),
                child: item,
              );
              return itemView;
            });
      },
    );

    return view;
  }
}
